package com.hex.ds.hdtp.core.web.data.configuration.controller;

import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.configuration.dto.requestDto.TableConvertRequestDto;
import com.hex.ds.hdtp.core.app.data.configuration.service.ITableConvertService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.configuration.converter.ITableConvertVoConverter;
import com.hex.ds.hdtp.core.web.data.configuration.vo.TableConvertVo;
import com.hex.ds.hdtp.core.web.data.configuration.vo.query.TableConvertQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Package com.hex.ds.hdtp.core.web.preparation.controller
 * @ClassName ConvertController
 * @Description 转换配置
 * @Author yh.wan
 * @Date 2023/8/7 15:53
 * @Version v1.0
 **/
@Slf4j
@RestController
@Api(tags = "00-准备工作：转换配置")
@RequestMapping("/api/convertConfig")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TableConvertController {

    private final ITableConvertService convertService;

    private final ITableConvertVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /**
     * @Method: toDto
     * @Param: [vo]
     * @Return: com.hex.ds.hdtp.core.app.preparation.dto.request.ConvertRequestDto
     * @Description: vo转RequestDto
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    private TableConvertRequestDto toDto(TableConvertVo convertVo) {
        return converter.toConvertRequestDto(convertVo);
    }

    /**
     * @Method: queryListByPage
     * @Param: [ConvertQueryVo, pageVo]
     * @Return: org.springframework.http.ResponseEntity
     * @Description: 转换配置分页查询
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    @GetMapping
    @ApiOperation("转换配置分页查询")
    public ResponseEntity queryListByPage(TableConvertQueryVo convertVo, PageVo pageVo) {
        log.info("进入到【ConvertController.queryListByPage】方法,参数：{}" + convertVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        TableConvertRequestDto requestDto = converter.toConvertRequestDtoForQuery(convertVo);
        return ResponseEntity.ok(convertService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @Method: add
     * @Param: [convertVo]
     * @Return: org.springframework.http.ResponseEntity
     * @Description: 新增转换配置
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    @Log("新增转换配置")
    @PostMapping
    @ApiOperation("新增转换配置")
    public ResponseEntity add(@RequestBody TableConvertVo convertVo) {
        log.info("进入到【ConvertController.add】方法,参数：{}", convertVo.toString());
        convertService.add(toDto(convertVo));
        return ResponseEntity.ok("新增成功");
    }

    /**
     * @Method: update
     * @Param: [convertVo]
     * @Return: org.springframework.http.ResponseEntity
     * @Description: 更新转换配置
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    @Log("更新转换配置")
    @PutMapping
    @ApiOperation("更新转换配置")
    public ResponseEntity update(@RequestBody TableConvertVo convertVo) {
        log.info("进入到【ConvertController.update】方法,参数：{}", convertVo.toString());
        convertService.modifyById(toDto(convertVo));
        return ResponseEntity.ok("更新成功");
    }

    /**
     * @Method: delete
     * @Param: [pkIds]
     * @Return: org.springframework.http.ResponseEntity
     * @Description:
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    @Log("删除转换配置")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("删除转换配置")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【ConvertController.delete】方法,参数：" + pkIds);
        convertService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @Method: download <br>
     * @Param: [request, response, convertQueryVo] <br>
     * @Return: void <br>
     * @Description：项目导出<br>
     * @Author yh.wan
     * @Date 2023/8/7 15:53
     * @Version v1.0
     **/
    @AnonymousAccess
    @Log("导出转换配置")
    @ApiOperation("导出转换配置")
    @GetMapping(value = "/download")
    public void download(HttpServletRequest request, HttpServletResponse response, TableConvertQueryVo convertQueryVo) {
        log.info("进入了【ConvertController.download】方法,参数: {}", convertQueryVo.toString());
        convertService.download(request, response, converter.toConvertRequestDtoForQuery(convertQueryVo));
    }

    /**
     * @param pkIds:
     * @return ResponseEntity
     * @author Liang Fuhao
     * @description 导出转换配置信息
     * @date 2023/12/14 14:39
     */
    @Log("导出转换配置信息")
    @ApiOperation("导出转换配置信息")
    @GetMapping(value = "/exportConvertConfig")
    public ResponseEntity exportConvertConfig(@RequestParam List<String> pkIds) {
        log.info("进入了【ConvertController.exportConvertConfig】方法,参数: {}", pkIds);
        ByteArrayResource bar = convertService.exportConvertConfig(pkIds);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=SourceInfoTemplate")
                .body(bar);
    }

    /**
     * @param file:
     * @return ResponseEntity
     * @author Liang Fuhao
     * @description 导入转换配置信息
     * @date 2023/12/14 14:39
     */
    @Log("导入转换配置信息")
    @ApiOperation("导入转换配置信息")
    @PostMapping(value = "/importConvertConfig")
    public ResponseEntity importConvertConfig(MultipartFile file) {
        log.info("进入了【ConvertController.download】方法,参数: {}", file);
        Map<String, Object> resultMap = convertService.importConvertConfig(file);
        return new ResponseEntity(resultMap, HttpStatus.OK);
    }
}
